*Set the path to your working directory 

clear all
global beg_path `"XXX"'
set more off

******** 
* ANES * 
********
 
use `"${beg_path}Data\Micro\ANES\anes_timeseries_cdf.dta"', clear

gen election_year = VCF0004
tab election_year, gen(dum_elect)

recode VCF0702 (2=1 "Voted") (1=0 "Abstained") (else=.), gen(turnout)
replace VCF0101 = . if VCF0101==0
gen age = VCF0101
lab var age "Age" 
gen yob = election_year-VCF0101
gen new_gen = yob >=1941
lab var new_gen "Affluent Generations" 

gen age_sq = age*age 
gen year= election_year


eststo clear
eststo M1: logit turnout new_gen age age_sq  i.year [pw=VCF0009z], cluster(election_year) // Pseudo R²: 0.0797
estadd local fixed "Yes" , replace


*Reported turnout
gen rep_turnout=.
levels election_year, local(election_year)
foreach i of numlist `election_year' {
cap tab turnout [aw=VCF0009z] if election_year ==`i', matcell(x) 
matrix list x
cap replace rep_turnout = x[2,1]/(x[2,1]+x[1,1]) if election_year ==`i'
}

*Real turnout
gen real_turnout = .
replace real_turnout =  52.2 if election_year == 1948
replace real_turnout =  62.3 if election_year == 1952
replace real_turnout =  43.5 if election_year == 1954
replace real_turnout =  60.2 if election_year == 1956
replace real_turnout =  45 if election_year == 1958
replace real_turnout =  63.8 if election_year == 1960
replace real_turnout =  47.7 if election_year == 1962
replace real_turnout =  62.8 if election_year == 1964                 
replace real_turnout =  48.7 if election_year == 1966                  
replace real_turnout =  62.5 if election_year == 1968
replace real_turnout =  47.3 if election_year == 1970
replace real_turnout =  56.5 if election_year == 1972
replace real_turnout =  39.1 if election_year == 1974
replace real_turnout =  54.8 if election_year == 1976
replace real_turnout =  39 if election_year == 1978
replace real_turnout =  54.2 if election_year == 1980
replace real_turnout =  42 if election_year == 1982
replace real_turnout =  55.2 if election_year == 1984
replace real_turnout =  38.1 if election_year == 1986
replace real_turnout =  52.8 if election_year == 1988
replace real_turnout =  38.4 if election_year == 1990
replace real_turnout =  58.1 if election_year == 1992
replace real_turnout =  41.1 if election_year == 1994
replace real_turnout =  51.7 if election_year == 1996
replace real_turnout =  38.1 if election_year == 1998
replace real_turnout =  54.2 if election_year == 2000
replace real_turnout =  39.5 if election_year == 2002
replace real_turnout =  60.1 if election_year == 2004
replace real_turnout =  61.6 if election_year == 2008
replace real_turnout =  41 if election_year == 2010
replace real_turnout =  58.6 if election_year == 2012
replace real_turnout =  60.2 if election_year == 2016
replace real_turnout = real_turnout/100

*Decline 1960s-2000s
di (62.8 + 62.5 + 63.8)/3 -(60.2 + 58.6)/2 //3.63

*Weight
gen weight_turnout = . 
levels election_year, local(election_year)
foreach i of numlist `election_year'  {
capture noisily display "Election" `i' 
replace weight_turnout=real_turnout/rep_turnout if election_year==`i' & turnout==1
replace weight_turnout=(1-real_turnout)/(1-rep_turnout) if election_year==`i' & turnout==0 
}
fre weight_turnout
gen final_weight=weight_turnout*VCF0009z

*All elections
logit turnout i.new_gen age age_sq  i.year [pw=final_weight], cluster(election_year)
margins, dydx(new_gen) // Yields 3.4 pp differences in the probability to vote 

gen filter_pres = inlist(election_year, 1948, 1952, 1956, 1960, 1964, 1968, 1972, 1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020)

*Presidential
logit turnout i.new_gen age age_sq  i.year [pw=final_weight] if filter_pres==1, cluster(election_year)
margins, dydx(new_gen) // Yields 3.12036


*Descriptive Stats *

cd `"${beg_path}Results\"'

lab var turnout "Turnout"
lab var year "Year"
sutex2 turnout  age new_gen year , tabular varlabels replace saving(Table_SM6.tex) minmax  //  title("Summary statistics - Micro Analysis") 


*************************************
**************** BES ****************
*************************************

cd `"${beg_path}Data\Micro\BES\To merge\"'

use 1964_to_merge.dta, clear

append using 1966_to_merge.dta 1970_to_merge.dta 1974feb_to_merge.dta 1974oct_to_merge.dta 1979_to_merge.dta 1983_to_merge.dta 1987_to_merge.dta 1992_to_merge.dta 1997_to_merge.dta ///
2001_to_merge.dta 2005_to_merge.dta 2010_to_merge.dta 2015_to_merge.dta 2017_to_merge.dta 


gen turnout = participation

gen year = election_year
replace year = 1974 if inlist(year, 197410, 197402) 
gen age = year - yob
lab var age "Age" 


gen age_sq = age*age
gen new_gen = yob>=1960
lab var new_gen "Affluent Generations" 

eststo M2: logit turnout new_gen age age_sq ib1974.year, cluster(year) // Pseudo R²:0.0476
estadd local fixed "Yes" , replace


*Descriptive Stats *
cd `"${beg_path}Results\"'

lab var turnout "Turnout"
lab var year "Year"
sutex2 turnout age new_gen year , tabular varlabels replace saving(Table_SM7.tex) minmax  //  title("Summary statistics - Micro Analysis") 



*Real turnout
gen real_turnout = .
replace real_turnout =  76 if election_year == 1966
replace real_turnout =  72.2 if election_year == 1970
replace real_turnout =  78.9 if election_year == 197402
replace real_turnout =  72.9 if election_year == 197410
replace real_turnout =  76.3 if election_year == 1979
replace real_turnout =  72.8 if election_year == 1983
replace real_turnout =  75.4 if election_year == 1987
replace real_turnout =  77.8 if election_year == 1992
replace real_turnout =  71.6 if election_year == 1997
replace real_turnout =  59.6 if election_year == 2001
replace real_turnout =  61.6 if election_year == 2005
replace real_turnout =  65.1 if election_year == 2010
replace real_turnout =  66.2 if election_year == 2015
replace real_turnout =  68.8 if election_year == 2017
replace real_turnout = real_turnout / 100 

*Decline 1960s-2000s
di 76-((65.1+66.2+68.3)/3) // 9.47 

*Reported turnout
gen rep_turnout =.
foreach i of numlist 1966 1970 197402 197410 1979 1983 1987 1992 1997 2001 2005 2010 2015 2017 {
cap tab turnout if election_year==`i', matcell(x) 
matrix list x
cap replace rep_turnout = x[2,1]/(x[2,1]+x[1,1]) if election_year ==`i'
}

*Weight
gen weight_turnout = . 
foreach i of numlist 1966 1970 197402 197410 1979 1983 1987 1992 1997 2001 2005 2010 2015 2017 {
capture noisily display "Election" `i' 
replace weight_turnout=real_turnout/rep_turnout if election_year==`i' & turnout==1
replace weight_turnout=(1-real_turnout)/(1-rep_turnout) if election_year==`i' & turnout==0 
}

logit turnout i.new_gen age age_sq ib1974.year [pw=weight_turnout], cluster(year) 
margins, dydx(new_gen) // Yields 7.6 pp differences in the probability to vote  
 	
*******
* CES *
*******

cd `"${beg_path}Data\Micro\Canada\To merge\"'

use 1965_to_merge.dta, clear

append using 1968_to_merge.dta 1974_to_merge.dta 1979_to_merge.dta 1980_to_merge.dta 1984_to_merge.dta 1988_to_merge.dta 1993_to_merge.dta 1997_to_merge.dta ///
2000_to_merge.dta 2004_to_merge.dta 2008_to_merge.dta 2011_to_merge.dta 2015_to_merge.dta 

drop if inlist(yob, .a, .b)

gen age = election_year - yob
lab var age "Age" 
gen age_sq = age*age
gen new_gen = yob>=1950
lab var new_gen "Affluent Generations" 
gen year = election_year
gen turnout = participation

eststo M3: logit turnout new_gen age age_sq ib1974.year, cluster(year) // Pseudo R²: 0.0797
estadd local fixed "Yes" , replace

*Descriptive Stats *
cd `"${beg_path}Results\"'

lab var turnout "Turnout"
lab var year "Year"
sutex2 turnout  age new_gen year , tabular varlabels  replace saving(Table_SM8.tex) minmax 

*Table 5

esttab M1 M2 M3 using Table_5.tex,  incelldelimite("\:")  stats(fixed N r2_p, labels("Election FE" "N" "Pseudo R2") ///
layout("\multicolumn{1}{c}{@}"  "\multicolumn{1}{c}{$@$}" "\multicolumn{1}{c}{$@$}") f(0 0 2))   one replace b(2) se(2) wide staraux  /// M4
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) drop( *year) nodep ///  
	order(new_gen age age_sq) ///  
 nolegend nonumbers coeflabels(new_gen "Affluent Generations" /// 
 age "Age" age_sq "Age x Age"  _cons "Constant") /// 
	mtitles("\shortstack{\\ANES}" "\shortstack{\\BES}" ///
	"\shortstack{\\CES}") varwidth(24) ///
	nonumbers nolegend note("") alignment(d{10})

di (80.1+80.3+75.9+75.7)/4 - (61.4+68)/2

*Real turnout
gen real_turnout = .
replace real_turnout =  75.9 if year == 1965
replace real_turnout =  75.7 if year == 1968
replace real_turnout =  71 if year == 1974
replace real_turnout =  75.7 if year == 1979
replace real_turnout =  69.3 if year == 1980
replace real_turnout =  75.3 if year == 1984
replace real_turnout =  75.3 if year == 1988
replace real_turnout =  69.6 if year == 1993
replace real_turnout =  67 if year == 1997
replace real_turnout =  61.3 if year == 2000
replace real_turnout =  60.9 if year == 2004
replace real_turnout =  58.8 if year == 2008
replace real_turnout =  61.4 if year == 2011
replace real_turnout =  68 if year == 2015
replace real_turnout = real_turnout / 100 

*Decline 
di (75.9+75.7)/2-(61.14+68)/2 // 11.23

*Reported turnout
gen rep_turnout =.
foreach i of numlist 1965 1968 1974 1979 1980 1984 1988 1993 1997 2000 2004 2008 2011 2015 {
cap tab turnout if year==`i', matcell(x) 
matrix list x
cap replace rep_turnout = x[2,1]/(x[2,1]+x[1,1]) if year ==`i'
}

*Weight
gen weight_turnout = . 
foreach i of numlist 1965 1968 1974 1979 1980 1984 1988 1993 1997 2000 2004 2008 2011 2015 {
capture noisily display "Election" `i' 
replace weight_turnout=real_turnout/rep_turnout if year==`i' & turnout==1
replace weight_turnout=(1-real_turnout)/(1-rep_turnout) if year==`i' & turnout==0 
}

logit turnout i.new_gen age age_sq ib1974.year [pw=weight_turnout], cluster(year) 
margins new_gen // Yields 8.5 pp differences in the probability to vote  	
	
* Latex	
	
estout M1 M2 M3 using Table_5.tex, incelldelimite("\:")   stats(fixed N r2_p, labels("Election FE" "N" "Pseudo R2") f(0 0 2)) replace b(%9.2g) se(2) wide staraux /// M4
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) drop( *year) nodep ///  
	order(new_gen age age_sq) ///  
 nolegend nonumbers coeflabels(new_gen "Affluent Generations" ///  
 age "Age" age_sq "Age x Age"  _cons "Constant") /// 
	mtitles("\shortstack{\\W\\ANES}" "\shortstack{\\X\\BES}" ///
	"\shortstack{\\Z\\CES}"  ) varwidth(24) ///
	nonumbers nolegend note("") alignment(d{10})

	
* Word 

esttab M1 M2 M3 using Table_5.rtf, one incelldelimiter(" ")  stats(fixed N r2_p, labels("Election FE" "N" "Pseudo R2") f(0 0 2) ) replace ///
b(2) se(2)  wide staraux /// 
	star(+ 0.1 * 0.05 ** 0.01 *** 0.001) eqlabels(none) drop( *year) nodep ///  
	order(new_gen age age_sq) ///  
 nolegend nonumbers coeflabels(new_gen "Affluent Generations" ///  
 age "Age" age_sq "Age x Age"  _cons "Constant") /// 
	mtitles("\shortstack{\\W\\ANES}" "\shortstack{\\X\\BES}" ///
	"\shortstack{\\Z\\CES}"  ) varwidth(24) ///
	nonumbers nolegend note("")









	

